package PTA.ch06;

import java.util.Scanner;


    import java.util.LinkedList;
import java.util.Scanner;

    interface GeneralStack<T> {
        T push(T item); //如果item为null，则不入栈直接返回null。如果栈满，也返回null。如果插入成功，返回item

        T pop();   //出栈，如果为空，则返回null。出栈时只移动栈顶指针，相应位置不置为null

        T peek();  //获得栈顶元素，如果为空，则返回null

        boolean empty(); //如果为空返回true

        int size();      //返回栈中元素个数
    }

    class ArrayListGeneralStack implements GeneralStack {
        private LinkedList<Object> list;

        public ArrayListGeneralStack() {
            list = new LinkedList<>();
        }

        @Override
        public String toString() {
            return list.toString();
        }

        @Override
        public Object push(Object item) {
            if (item == null) {
                return null;
            }
            list.add(item);
            return item;
        }

        @Override
        public Object pop() {
            if (list.size() == 0) {
                return null;
            }
            Object topNum = list.getLast();
            list.removeLast();
            return topNum;
        }

        @Override
        public Object peek() {
            if (list.size() == 0) {
                return null;
            }
            return list.getLast();
        }

        @Override
        public boolean empty() {
            return list.size() == 0;
        }

        @Override
        public int size() {
            return list.size();
        }
    }

    class Car {
        private int id;
        private String name;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Car [" +
                    "id=" + id +
                    ", name=" + name +
                    ']';
        }
    }

    class Main {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            while (true) {
                String choice = scanner.next();
                if (choice.equals("quit")) {
                    break;
                }
                switch (choice) {
                    case "Integer":
                        System.out.println("Integer Test");
                        ArrayListGeneralStack algsInt = new ArrayListGeneralStack();
                        int mInt = scanner.nextInt();
                        int nInt = scanner.nextInt();
                        while (mInt-- > 0) {
                            int pushNum = scanner.nextInt();
                            System.out.println("push:" + pushNum);
                            algsInt.push(pushNum);
                        }
                        while (nInt-- > 0) {
                            System.out.println("pop:" + algsInt.pop());
                        }
                        System.out.println(algsInt);
                        int sumInt = 0;
                        int sizeInt = algsInt.size();
                        for (int i = 0; i < sizeInt; i++) {
                            sumInt += Integer.parseInt(algsInt.peek().toString());
                            algsInt.pop();
                        }
                        System.out.println("sum=" + sumInt);
                        System.out.println(algsInt.getClass().getInterfaces()[0]);
                        break;
                    case "Double":
                        System.out.println("Double Test");
                        ArrayListGeneralStack algsDouble = new ArrayListGeneralStack();
                        int mDouble = scanner.nextInt();
                        int nDouble = scanner.nextInt();
                        while (mDouble-- > 0) {
                            double pushNum = scanner.nextDouble();
                            System.out.println("push:" + pushNum);
                            algsDouble.push(pushNum);
                        }
                        while (nDouble-- > 0) {
                            System.out.println("pop:" + algsDouble.pop());
                        }
                        System.out.println(algsDouble);
                        double sumDouble = 0;
                        int sizeDouble = algsDouble.size();
                        for (int i = 0; i < sizeDouble; i++) {
                            sumDouble += Double.parseDouble(algsDouble.peek().toString());
                            algsDouble.pop();
                        }
                        System.out.println("sum=" + sumDouble);
                        System.out.println(algsDouble.getClass().getInterfaces()[0]);
                        break;
                    case "Car":
                        System.out.println("Car Test");
                        ArrayListGeneralStack algsCar = new ArrayListGeneralStack();
                        int mCar = scanner.nextInt();
                        int nCar = scanner.nextInt();
                        while (mCar-- > 0) {
                            int id = scanner.nextInt();
                            String name = scanner.next();
                            Car car = new Car();
                            car.setId(id);
                            car.setName(name);
                            System.out.println("push:" + car);
                            algsCar.push(car);
                        }
                        while (nCar-- > 0) {
                            System.out.println("pop:" + algsCar.pop());
                        }
                        System.out.println(algsCar);
                        int sizeCar = algsCar.size();
                        for (int i = 0; i < sizeCar; i++) {
                            Car car=(Car) algsCar.pop();
                            System.out.println(car.getName());
                        }
                        System.out.println(algsCar.getClass().getInterfaces()[0]);
                        break;
                }
            }
        }
    }

